﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DeliveryService
{
    public class BLShoppingCartFoods
    {
        //AddFoodsToShoppingCart{studentId=12; foodsId=1; foodsAmount=1; }。
        public bool AddFoodsToShoppingCart(int studentId, int foodsId, int foodsAmount)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                try
                {
                    var query = db.ShoppingCartFoods.SingleOrDefault(s => s.Student.Students_Id == studentId && s.Food.Foods_Id == foodsId);
                    if (query != null)
                    {
                        query.Foods_Amount += foodsAmount;
                    }
                    else
                    {
                        ShoppingCartFood S = new ShoppingCartFood();
                        S.Student = db.Students.SingleOrDefault(s => s.Students_Id == studentId);
                        S.Food = db.Foods.SingleOrDefault(g => g.Foods_Id == foodsId);
                        S.Foods_Amount = foodsAmount;
                        db.ShoppingCartFoods.InsertOnSubmit(S);
                    }
                    db.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;

                }
            }
        }

        public List<MShoppingCartFoods> GetShoppingCartFoodsByStudentId(int studentId)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.ShoppingCartFoods.Where(s => s.Students_Id.HasValue && s.Students_Id == studentId)
                                                .Select(s => new MShoppingCartFoods
                                                  {
                                                      StudentId = s.Students_Id.Value,
                                                      FoodsId = s.Food.Foods_Id,
                                                      FoodssName = s.Food.Foods_Name,
                                                      Id = s.ID,
                                                      RestaurantName = s.Food.Restaurant.Restaurant_Name,
                                                      IsBusy = s.Food.Restaurant.IsBusy.HasValue ? (bool)s.Food.Restaurant.IsBusy.Value : false,
                                                      FoodsAmount = s.Foods_Amount.HasValue ? s.Foods_Amount.Value : 0
                                                  });
                return query.ToList();
            }
        }

        public bool DeleteFoodsFromShoppingCart(int studentId, int foodsId)
        {
            try
            {
                using (DeliveryDBDataContext db = new DeliveryDBDataContext())
                {
                    var sc = db.ShoppingCartFoods.Where(s => s.Student.Students_Id == studentId && s.Food.Foods_Id == foodsId);
                    db.ShoppingCartFoods.DeleteAllOnSubmit(sc);
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {

                return false;
            }
        }

        public bool DeleteFoodsListFromShoppingCart(int studentId, string foodsIds)
        {
            try
            {
                using (DeliveryDBDataContext db = new DeliveryDBDataContext())
                {
                    var ary = foodsIds.Split(',').Select(x => int.Parse(x));
                    var sc = db.ShoppingCartFoods.Where(s => s.Student.Students_Id == studentId && ary.Contains(s.Food.Foods_Id));
                    db.ShoppingCartFoods.DeleteAllOnSubmit(sc);
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {

                return false;
            }
        }

        public bool DeleteFoodsListFromShoppingCart(string shopingCartIdList)
        {
            try
            {
                using (DeliveryDBDataContext db = new DeliveryDBDataContext())
                {
                    var ary = shopingCartIdList.Split(',').Select(x => int.Parse(x));
                    var sc = db.ShoppingCartFoods.Where(s => ary.Contains(s.ID));
                    db.ShoppingCartFoods.DeleteAllOnSubmit(sc);
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {

                return false;
            }
        }

        public bool EditShoppingCartFoods(int shoppingCartFoodsId, int ammount)
        {
            try
            {
                using (DeliveryDBDataContext db = new DeliveryDBDataContext())
                {
                    var sc = db.ShoppingCartFoods.Single(s => s.ID == shoppingCartFoodsId);
                    sc.Foods_Amount = ammount;
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
    }

}